package com.mbc.rmt.manager;

import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.DBManager;
import com.mbc.common.dao.DBManagerFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.DepartmentDAO;
import com.mbc.rmt.dao.IAIMSDepartmentDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.DepartmentDataList;
import com.mbc.rmt.entities.Department;
import com.mbc.rmt.entities.DepartmentRpt;

public class DepartmentMgr extends AbstractMgr{
	
	private DepartmentDAO departmentDAO;
	
	private IAIMSDepartmentDAO iaimsdepartmentDAO;
	
	private LogDAO logDAO;
	
	public DepartmentMgr(){}
	
	public DepartmentMgr(String sessionId){
		this.sessionId = sessionId;
	}
	public void create(Department entity)throws Exception{
		DBManager l_DBManager = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		openConnection();
		try{
			l_DBManager.beginTransaction();
			beginTransaction();
			
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(dbManager.getConnection());
			iaimsdepartmentDAO = RemittanceDAOFactory.getIAIMSDepartmentDAO(l_DBManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			entity.setDepartment_id(KeyGenerator.generateKey(getConnection()));
			
			departmentDAO.create(entity);
			
			Log logentity = new Log();
			logentity.setTablename("Department");
			logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			logentity.setKey(entity.getDepartment_id());
			logentity.setUserid(entity.getUserid());
			logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(logentity);
			
			if(iaimsdepartmentDAO.findByCode(entity.getDepartment_code(), entity.getBranch_id()) == null)
				iaimsdepartmentDAO.create(entity);
			
			l_DBManager.commitTransaction();
			commintTransaction();
		} catch(Exception e){
			l_DBManager.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally{
			l_DBManager.closeConnection();
			closeConnection();
		}
	}
	public void update(Department entity)throws Exception{
		DBManager l_DBManager = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		openConnection();
		try{
			l_DBManager.beginTransaction();
			beginTransaction();
			
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(dbManager.getConnection());
			iaimsdepartmentDAO = RemittanceDAOFactory.getIAIMSDepartmentDAO(l_DBManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			departmentDAO.update(entity);
			iaimsdepartmentDAO.update(entity);
			
			Log l_log = new Log();
			l_log.setTablename("Department");
			l_log.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_log.setKey(entity.getDepartment_id());
			l_log.setUserid(entity.getUserid());
			l_log.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_log);
			
			l_DBManager.commitTransaction();
			commintTransaction();
		} catch(Exception e){
			l_DBManager.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally {
			l_DBManager.closeConnection();
			closeConnection();
		}
	}
	public void delete(long key, long userid)throws Exception {
		DBManager l_DBManager = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		openConnection();
		try{
			l_DBManager.beginTransaction();
			beginTransaction();
			
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(dbManager.getConnection());
			iaimsdepartmentDAO = RemittanceDAOFactory.getIAIMSDepartmentDAO(l_DBManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_log = new Log();
			l_log.setTablename("Department");
			l_log.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_log.setKey(key);
			l_log.setUserid(userid);
			l_log.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_log);
			
			departmentDAO.delete(key);
			iaimsdepartmentDAO.delete(key);
			
			l_DBManager.commitTransaction();
			commintTransaction();
		} catch(Exception e){
			l_DBManager.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally {
			l_DBManager.closeConnection();
			closeConnection();
		}
	}
	public Department findByCode(String code,long branchId)throws Exception {
		openConnection();
		try{
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(dbManager.getConnection());
			return departmentDAO.findByCode(code,branchId);
		} catch(Exception e){
			throw e;
		} finally{
			closeConnection();
		}
	}
	
	public DepartmentDataList findDepartments(Department cri)throws Exception {
		openConnection();
		try{
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(dbManager.getConnection());
			
			return departmentDAO.findDepartments(cri);
		} catch (Exception e){
			throw e;
		} finally {
			closeConnection();
		}
	}
	public DepartmentDataList getDepartmentList(DepartmentRpt department)throws Exception{
		openConnection();
		try {
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(dbManager.getConnection());
			
			return departmentDAO.getDepartmentList(department);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public JasperPrint prepareDepartmentReport(DepartmentRpt department) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(getConnection());
//			ResultSet l_rs = DepartmentDAOImpls.prepareDepartmentReport(from, to,getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(departmentDAO.prepareDepartmentReport(department));

			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH+"reports/Department.jasper");
			
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}

}
